package com.tensquare.qa.service;

import com.tensquare.qa.dao.ProbelemDao;
import com.tensquare.qa.pojo.Problem;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class ProblemService {

    @Autowired
    private ProbelemDao probelemDao;


    //最新回答列表（sql查询，使用本地sql查询）
    //最新回复的问题显示在上方， 按回复时间降序排序。
    public PageResult<Problem> findNewProblemList(String label, int page, int size) {
        Page<Problem> problemsByLabelid = probelemDao.findProblemsByLabelid(label, PageRequest.of(page-1,size));
        System.out.println("problemsByLabelid = " + problemsByLabelid);
        return new PageResult<>(problemsByLabelid.getTotalElements(),problemsByLabelid.getContent());
    }

    //热门问答列表
    //按回复数降序排序
    public PageResult<Problem> findhostProblem(String label, int page, int size) {
        Page<Problem> hostProblemByLabelid = probelemDao.findHostProblemByLabelid(label, PageRequest.of(page-1,size));
        System.out.println("hostProblemByLabelid = " + hostProblemByLabelid);
        return new PageResult<>(hostProblemByLabelid.getTotalElements(),hostProblemByLabelid.getContent());
    }
}
